<?php


namespace app\controller;
use \app\model\Aizone;
use app\model\MqttUserManage;
use think\facade\Log;
use think\Image;

/**
 * 省厅下发数据
 * @package app\controller
 */
class Apply extends BackendController {
    protected $noNeedRight=['*'];
    protected $noNeedLogin=['*'];

    public function access($areaname){
        $url=request()->url();
        $urls=explode("/",$url);
        Log::info("url:::".$url);
        $areaname=$urls[count($urls)-1];
        $aizone=Aizone::where("name_code",$areaname)->find();
        if(!$aizone) $this->success('code_error');
        $where_from=$aizone->where_from;
        $doc = file_get_contents('php://input');
        $ip_address= request()->ip();
        Log::info("input:::".$doc);
        $json_data=json_decode($doc);

        if(!$json_data) {
            Aizone::where('name_code',$areaname)->inc('online_total');
            return $this->success('online');
        }
        $pad_status=array();
        $check_same=0;

        foreach($json_data as $info){
            if(isset($info->ywlsh))   $ywlsh = $info->ywlsh; //UUID 业务流水号
            if(isset($info->id))   $ywlsh = $info->id; //UUID 业务流水号

            $address = $info->address; //地址
            $idCard = $info->idCard; //身份证
            $name = $info->name;//姓名
            $photo = $info->photo; //照片base64
            $source = $info->source; //区分数据来源(01：手机扫码填报，02：AI能力层小区管理后台填报（后续实现）)
            $type = $info->type; //人员类型，01住户，02租户，03物业人员，04服务人员，05来访人员
            $phone = '';
            if(isset($info->phone)) $phone = $info->phone;

            $communityCode='';
            $isAuthsPass='';
            $isOldAndChild='';


            if(isset($info->communityCode)) $communityCode=$info->communityCode;
            if(isset($info->isAuthsPass)) $isAuthsPass=$info->isAuthsPass;
            if(isset($info->isOldAndChild)) $isOldAndChild=$info->isOldAndChild;



            $check_same_status=MqttUserManage::where('address_id',$areaname)->where('idCard',$idCard)->where('type',$type)->where('ywlsh',$ywlsh)->find();

            if($check_same_status) {
                if($type=='05'){//访客,单独授权
                    $pad_push_status=array(
                        'id'=>$ywlsh,
                        'statusName'=>'访客-审核通过',
                        'statusCode'=>'02',
                        'communityCode'=>$areaname,
                        'period'=>'1天',
                    );
                } else {
                    $pad_push_status=array(
                        'id'=>$ywlsh,
                        'statusName'=>'审核通过',
                        'statusCode'=>'02',
                        'communityCode'=>$areaname,
                        'period'=>'always',
                    );
                }
                array_push($pad_status,$pad_push_status);
                continue;
            }
            $zone_name =$aizone->name;
            $insert_sql= array(
                'address_wherefrom'=>$where_from,
                'ywlsh'=>$ywlsh,
                'address'=>$address,
                'address_id'=>$areaname,
                'idCard'=>$idCard,
                'name'=>$name,
                'phone'=>$phone,
                'source'=>$source,
                'type'=>$type,
                'zone_name'=>$zone_name,
                'user_id'=>'',
                'status'=>0,
                'capture'=>1,
                'communityCode'=>$communityCode,
                'isAuthsPass'=>$isAuthsPass,
                'isOldAndChild'=>$isOldAndChild,
                'hidedevice'=>0,
                'ai_version'=>3,
                'callback_status'=>0
            ); //登录
            $now=MqttUserManage::create($insert_sql);
            $now->photo=save_base_img($now->id,$photo,config('capture.apply_path').date('Y-m').'/'.date('d').'/');
            $image = Image::open($now->photo);
            $image->thumb(500,ceil(500*$image->height()/$image->width()))->save($now->photo);
            $now->save();
            $this->success('',['msg' => "success",'code'=>200]);
           /* array_push($pad_status,$res);
            Cache::set('mtqq_user_'.$now->id,json_encode($res));*/
        }
        return $this->success('online');
    }

    public function reply(){
        $lists=MqttUserManage::where("callback_status",'=',0)->limit(1)->select();
        $_manage=new MqttUserManage();
        //$_manage->callback(121782);
        foreach ($lists as $list){
            $_manage->callback($list['id']);
            //break;
        }
    }

    public function reply1($id){
        $lists=MqttUserManage::where("callback_status",'=',0)->select();
        $_manage=new MqttUserManage();
        $_manage->callback($id);die();
        foreach ($lists as $list){
            $_manage->callback($list['id']);
            break;
        }
    }

}